

@extends('layouts/app')

@section('content')

    <div class="weui-msg">
        <div class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></div>
        <div class="weui-msg__text-area">
            <h2 class="weui-msg__title">微信支付</h2>

        </div>
        <div class="weui-msg__opr-area">
            <p class="weui-btn-area">
                <a href="javascript:;" class="weui-btn weui-btn_primary payment" style="width: 5.6rem" onclick="pay()">确认支付</a>

            </p>
        </div>

    </div>

    <script src="/jquery/dist/jquery.js"></script>
    <script type="text/javascript">
        var order_id = getRequest().order_id,
                code    = getRequest().code,
                pay_way  = getRequest().pay_way;

        function getRequest () {
            var myRequest = {};
            var url = window.location.search;
            if (url.indexOf('?') !== -1) {
                var myUrl = url.substr(1);
                var arr = myUrl.split('&');
                for (var i = 0; i < arr.length; i++) {
                    var name = arr[i].split('=')[0];
                    var value = arr[i].split('=')[1];
                    myRequest[name] = value;
                }
            }
            return myRequest;
        }

        function pay(){
            var data = {};
            data['order_id']=order_id;
            data['pay_way']=pay_way;
            data['code']=code;
            $.post("/api/payh5", data, function (r) {
                if (typeof r === 'string') {
                    r = JSON.parse(r);
                }
                console.log(r)
                if(r.code==200){
                    onBridgeReady(r.data);
                }else{
                    alert(r.info);
                }
            })
        }

        function onBridgeReady(paydata){
            WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', {
                        "appId":paydata.appId,        //公众号名称，由商户传入
                        "timeStamp":paydata.timeStamp,//时间戳，自1970年以来的秒数
                        "nonceStr":paydata.nonceStr,  //随机串
                        "package":paydata.package,
                        "signType":paydata.signType,  //微信签名方式：
                        "paySign":paydata.paySign     //微信签名
                    },
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok"){
                            alert("支付成功");
                            window.location.href="/mobile/order-detail?id="+order.order.id+"&order_type="+order.order_type;
                        }else{
                            alert("支付失败");
                            window.location.href="/mobile/index";
                        }
                    });
        }

    </script>

@endsection
@extends('layouts/footer')
